package com.cdu.supermarket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cdu.supermarket.entity.domain.GoodsAttribute;
import com.cdu.supermarket.entity.domain.GoodsAttributeOption;
import com.cdu.supermarket.mapper.GoodsAttributeMapper;
import com.cdu.supermarket.mapper.GoodsAttributeOptionMapper;
import com.cdu.supermarket.service.IGoodsAttributeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsAttributeServiceImpl extends ServiceImpl<GoodsAttributeMapper, GoodsAttribute> implements IGoodsAttributeService {
    
    @Resource
    private GoodsAttributeOptionMapper optionMapper;
    
    @Override
    public List<GoodsAttribute> listByCategoryId(Long categoryId) {
        QueryWrapper<GoodsAttribute> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId)
               .or()
               .isNull("category_id")
               .orderByAsc("sort_order");
        
        List<GoodsAttribute> attributes = this.list(wrapper);
        
        // 加载枚举类型属性的选项
        for (GoodsAttribute attribute : attributes) {
            if ("enum".equals(attribute.getAttrType())) {
                QueryWrapper<GoodsAttributeOption> optionWrapper = new QueryWrapper<>();
                optionWrapper.eq("attr_id", attribute.getId())
                           .orderByAsc("sort_order");
                List<GoodsAttributeOption> options = optionMapper.selectList(optionWrapper);
                attribute.setOptions(options);
            }
        }
        
        return attributes;
    }
    
    @Override
    @Transactional
    public void saveBatchOptions(Long attrId, List<String> options) {
        // 先删除原有选项
        QueryWrapper<GoodsAttributeOption> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id", attrId);
        optionMapper.delete(wrapper);
        
        // 添加新选项
        if (options != null && !options.isEmpty()) {
            List<GoodsAttributeOption> optionList = new ArrayList<>();
            for (int i = 0; i < options.size(); i++) {
                GoodsAttributeOption option = new GoodsAttributeOption();
                option.setAttrId(attrId);
                option.setOptionValue(options.get(i));
                option.setSortOrder(i);
                optionList.add(option);
            }
            
            // 批量插入
            for (GoodsAttributeOption option : optionList) {
                optionMapper.insert(option);
            }
        }
    }

    @Override
    public List<GoodsAttributeOption> getAttributeOptions(Long attrId) {
        QueryWrapper<GoodsAttributeOption> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id", attrId)
              .orderByAsc("sort_order");
        return optionMapper.selectList(wrapper);
    }
} 